<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增呼入大模型配置')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-inboundllm-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required" th:text="#{inboundllm.form.llmAccountId}"></label>
                    <div class="col-sm-8">
                        <select name="llmAccountId" class="form-control" required>
                            <option value="" th:text="#{inboundllm.form.llmAccountId.empty}"></option>
                            <!-- 大模型底座选项将通过接口动态加载 -->
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label" th:text="#{inboundllm.form.voiceCode}"></label>
                    <div class="col-sm-8">
                        <select name="voiceCode" class="form-control">
                            <option value="" th:text="#{inboundllm.form.voiceCode.empty}" ></option>
                            <!-- 音色选项将通过接口动态加载 -->
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required" th:text="#{inboundllm.form.callee}"></label>
                    <div class="col-sm-8">
                        <input name="callee" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required" th:text="#{inboundllm.form.serviceType}"></label>
                    <div class="col-sm-8">
                        <select name="serviceType" class="form-control" required>
                            <option value="ai" th:text="#{inboundllm.form.serviceTypeAI}"></option>
                            <option value="acd" th:text="#{inboundllm.form.serviceTypeACD}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group" >
                    <label class="col-sm-3 control-label is-required" th:text="#{inboundllm.form.groupId}"></label>
                    <div class="col-sm-8">
                        <select name="groupId" class="form-control" required>
                            <option value="" th:text="#{inboundllm.form.groupId.empty}"></option>
                            <!-- 业务组选项将通过接口动态加载 -->
                        </select>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "aicall/inboundllm"

        $(function() {
            // 加载大模型底座下拉框数据
            $.ajax({
                url: ctx + "aicall/account/all",
                success: function(rsp) {
                    var llmAccountSelect = $('select[name="llmAccountId"]');
                    llmAccountSelect.empty();
                    llmAccountSelect.append('<option value="">' + i18n('inboundllm.form.llmAccountId.empty') + '</option>');
                    rsp.data.forEach(function(llmAccount) {
                        llmAccountSelect.append($("<option>").attr("value", llmAccount.id).text(llmAccount.name));
                    });
                }
            });

            // 加载音色下拉框数据
            $.ajax({
                url: ctx + "aicall/ttsAliyun/all",
                success: function(rsp) {
                    var voiceSelect = $('select[name="voiceCode"]');
                    voiceSelect.empty();
                    voiceSelect.append('<option value="">' + i18n('inboundllm.form.voiceCode.empty') + '</option>');
                    rsp.data.forEach(function(voice) {
                        voiceSelect.append($("<option>").attr("value", voice.voiceCode).text(voice.voiceName));
                    });
                }
            });

            // 加载业务组下拉框数据
            $.ajax({
                url: ctx + "cc/bizgroup/all",
                success: function(rsp) {
                    var groupSelect = $('select[name="groupId"]');
                    groupSelect.empty();
                    groupSelect.append('<option value="">' + i18n('inboundllm.form.groupId.empty') + '</option>');
                    rsp.data.forEach(function(bizGroup) {
                        groupSelect.append($("<option>").attr("value", bizGroup.groupId).text(bizGroup.bizGroupName));
                    });
                }
            });

            $('input[name="callee"]').rules('add', {
                remote: {
                    url: prefix + '/checkCallee',
                    type: 'get',
                    data: {
                        callee: function () {
                            return $('input[name="callee"]').val();
                        }
                    },
                    dataFilter: function (resp) {
                        // 根据后端返回格式做判断
                        // 假设返回 {"data":true/false}
                        var json = JSON.parse(resp);
                        return json.data ? 'true' : '"该号码不可用"';
                    }
                },
                messages: {
                    remote: '该号码不可用'
                }
            });

        })
        $("#form-inboundllm-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-inboundllm-add').serialize());
            }
        }
    </script>
</body>
</html>